package com.cskaoyan.controller;

import com.cskaoyan.bean.common.CommonData;
import com.cskaoyan.bean.vo.BaseRespVo;
import com.cskaoyan.domain.MarketCategory;
import com.cskaoyan.domain.MarketGoods;
import com.cskaoyan.service.CategoryService;
import com.cskaoyan.service.GoodsService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/wx/goods")
public class WxGoodsController {
    @Autowired
    GoodsService goodsService;
    @Autowired
    CategoryService categoryService;
    @GetMapping("/count")
    public BaseRespVo countGoods(){
        Integer allGoodsCount = goodsService.getAllGoodsCount();
        return BaseRespVo.ok(allGoodsCount);
    }
    @GetMapping("/index")
    public BaseRespVo indexGoods(){
        return null;
    }
    @GetMapping("/detail")
    public BaseRespVo detailGoods(Integer id){
        Map<String, Object> goodsDetails = goodsService.getGoodsDetails(id);
        return BaseRespVo.ok(goodsDetails);
    }
    @GetMapping("/related")
    public BaseRespVo related(Integer id) {
        BaseRespVo data = goodsService.selectRelatedById(id);
        return data;
    }
    @RequestMapping("category")
    public BaseRespVo category(Integer id){
        Map<String,Object> map = new HashMap<>();
        MarketCategory categoryById = categoryService.getCategoryById(id);
        Integer pid = categoryById.getPid();
        if(pid==0){
            List<MarketCategory> categoryByPid = categoryService.getCategoryByPid(id);
            MarketCategory marketCategory = categoryByPid.get(0);
            map.put("brotherCategory",categoryByPid);
            map.put("currentCategory",marketCategory);
            map.put("parentCategory",categoryById);
        }else {
            MarketCategory categoryById1 = categoryService.getCategoryById(pid);
            Integer id1 = categoryById1.getId();
            List<MarketCategory> categoryByPid = categoryService.getCategoryByPid(id1);
            map.put("brotherCategory",categoryByPid);
            map.put("currentCategory",categoryById);
            map.put("parentCategory",categoryById1);
        }
        return BaseRespVo.ok(map);
    }
    @RequestMapping("list")
    public BaseRespVo list( String keyword,Integer categoryId ,Integer brandId,Integer page ,Integer limit,String sort,String order){
        Map<String,Object> map=new HashMap<>();
        if(brandId==null && categoryId!=null && keyword==null){
            PageHelper.startPage(page,limit);
            List<MarketGoods> goodsByCategoryId = goodsService.getGoodsByCategoryId(categoryId);
            List<MarketCategory> categories = categoryService.getCategories();
            List list = com.cskaoyan.utils.ListLimit.limitList(categories, "19");

            PageInfo<MarketGoods> objectPageInfo = new PageInfo<>(goodsByCategoryId);

            CommonData data = CommonData.data(objectPageInfo);
            map.put("filtercategory",list);
            map.put("limit",data.getLimit());
            map.put("list",data.getList());
            map.put("page",data.getPage());
            map.put("pages",data.getPages());
            map.put("total",data.getTotal());
        }else if(brandId!=null && categoryId==null &&keyword==null){
            PageHelper.startPage(page,limit);
            List<MarketGoods> goodsByBrandId = goodsService.getGoodsByBrandId(brandId);
            List<MarketCategory> categories = categoryService.getCategories();
            List list = com.cskaoyan.utils.ListLimit.limitList(categories, "7");

            PageInfo<MarketGoods> objectPageInfo = new PageInfo<>(goodsByBrandId);

            CommonData data = CommonData.data(objectPageInfo);
            map.put("filtercategory",list);
            map.put("limit",data.getLimit());
            map.put("list",data.getList());
            map.put("page",data.getPage());
            map.put("pages",data.getPages());
            map.put("total",data.getTotal());
        }else if(keyword!=null){
            PageHelper.startPage(page,limit);
            List<MarketGoods> goodsLike = goodsService.getGoodsLike(keyword,sort,order);

            PageInfo<MarketGoods> objectPageInfo = new PageInfo<>(goodsLike);
            CommonData data = CommonData.data(objectPageInfo);
            ArrayList<String> list = new ArrayList<>();
            for (MarketGoods marketGoods : goodsLike) {
                Integer categoryId1 = marketGoods.getCategoryId();
                MarketCategory categoryById = categoryService.getCategoryById(categoryId1);
                String name = categoryById.getName();
                list.add(name);
            }
            List<String> collect = list.stream().distinct().collect(Collectors.toList());
            ArrayList<MarketCategory> objects = new ArrayList<>();
            for (String s : collect) {
                MarketCategory categoryByname = categoryService.getCategoryByname(s);
                objects.add(categoryByname);
            }

            map.put("filtercategory",objects);
            map.put("limit",limit);
            map.put("list",data.getList());
            map.put("page",page);
            map.put("pages",data.getPages());
            map.put("total",data.getTotal());
        }
        return BaseRespVo.ok(map);
}
}